home *** CD-ROM | disk | FTP | other *** search
- /***************************************
- $Header: /home/amb/cxref/RCS/warn-raw.c 1.13 1996/02/24 14:54:12 amb Exp $
-
- C Cross Referencing & Documentation tool. Version 1.0
-
- Writes the raw information and / or warnings out.
- ******************/ /******************
- Written by Andrew M. Bishop
-
- This file Copyright 1995,96 Andrew M. Bishop
- It may be distributed under the GNU Public License, version 2, or
- any higher version. See section COPYING of the GNU Public license
- for conditions under which this file may be redistributed.
- ***************************************/
-
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- #include "datatype.h"
- #include "cxref.h"
- #include "memory.h"
-
- static void WriteWarnRawFilePart(File file);
- static void WriteWarnRawInclude(Include inc);
- static void WriteWarnRawSubInclude(Include inc,int depth);
- static void WriteWarnRawDefine(Define def);
- static void WriteWarnRawTypedef(Typedef type);
- static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base);
- static void WriteWarnRawVariable(Variable var);
- static void WriteWarnRawFunction(Function func);
-
- /*+ Output option. +*/
- extern int option_warn,option_raw,option_xref,option_index;
-
- /*+ The name of the current file. +*/
- static char* filename=NULL;
-
- /*++++++++++++++++++++++++++++++++++++++
- Write the raw / warning output for a complete File structure and all components.
-
- File file The File structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- void WriteWarnRawFile(File file)
- {
- Include inc =file->includes;
- Define def =file->defines;
- Typedef type=file->typedefs;
- Variable var=file->variables;
- Function func=file->functions;
-
- filename=file->name;
-
- /*+ The file structure is broken into its components and they are each written out. +*/
-
- WriteWarnRawFilePart(file);
-
- while(inc)
- {
- WriteWarnRawInclude(inc);
- inc=inc->next;
- }
-
- while(def)
- {
- WriteWarnRawDefine(def);
- def=def->next;
- }
-
- while(type)
- {
- WriteWarnRawTypedef(type);
- type=type->next;
- }
-
- while(var)
- {
- WriteWarnRawVariable(var);
- var=var->next;
- }
-
- while(func)
- {
- WriteWarnRawFunction(func);
- func=func->next;
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a File structure out.
-
- File file The File structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawFilePart(File file)
- {
- int i;
-
- if(option_raw)
- printf("\nFILE : '%s'\n",file->name);
-
- if(file->comment && option_raw)
- printf("<<<\n%s\n>>>\n",file->comment);
-
- if(option_warn&WARN_COMMENT && !file->comment)
- printf("Warning %16s : File does not have a comment.\n",filename);
-
- if(option_xref&XREF_FILE)
- {
- if(option_raw)
- for(i=0;i<file->inc_in.n;i++)
- printf("Included in %s\n",file->inc_in.s[i]);
-
- if(option_warn&WARN_XREF)
- {
- int len=strlen(file->name)-2;
- if(!file->inc_in.n && !strcmp(&file->name[len],".h"))
- printf("Warning %16s : Header file %s is not included in any files.\n",filename,file->name);
- if( file->inc_in.n && !strcmp(&file->name[len],".c"))
- printf("Warning %16s : Source file %s is included in another file.\n",filename,file->name);
- }
- }
-
- if(option_xref&XREF_FUNC)
- for(i=0;i<file->f_refs.n;i++)
- {
- if(option_raw)
- printf("References Function %s\n",file->f_refs.s[i]);
- if(option_warn&WARN_XREF && !strchr(file->f_refs.s[i],':'))
- printf("Warning %16s : File references function %s() whose definition is unknown.\n",filename,file->f_refs.s[i]);
- }
-
- if(option_xref&XREF_VAR)
- for(i=0;i<file->v_refs.n;i++)
- {
- if(option_raw)
- printf("References Variable %s\n",file->v_refs.s[i]);
- if(option_warn&WARN_XREF && !strchr(file->v_refs.s[i],':'))
- printf("Warning %16s : File references variable %s whose definition is unknown.\n",filename,file->v_refs.s[i]);
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write an Include structure out.
-
- Include inc The Include structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawInclude(Include inc)
- {
- if(option_raw)
- printf("\nINCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));
-
- if(inc->comment && option_raw)
- printf("<<<\n%s\n>>>\n",inc->comment);
- if(option_warn&WARN_COMMENT && !inc->comment)
- printf("Warning %16s : #Include %s does not have a comment.\n",filename,inc->name);
-
- if(option_raw && inc->includes)
- WriteWarnRawSubInclude(inc->includes,1);
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write an Sub-Include structure out.
-
- Include inc The Include structure to output.
-
- int depth The depth of the include hierarchy.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawSubInclude(Include inc,int depth)
- {
- int i;
-
- while(inc)
- {
- for(i=0;i<depth;i++) printf(" ");
- printf("INCLUDES : '%s' [%s file]\n",inc->name,(inc->scope==GLOBAL?"System":"Local"));
-
- if(inc->includes)
- WriteWarnRawSubInclude(inc->includes,depth+1);
-
- inc=inc->next;
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a Define structure out.
-
- Define def The Define structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawDefine(Define def)
- {
- int i;
-
- if(option_raw)
- {
- printf("\nDEFINES : '%s' ",def->name);
-
- if(def->value)
- printf("= %s",def->value);
-
- if(def->args.n)
- {
- printf("(");
- for(i=0;i<def->args.n;i++)
- printf(i?",%s":"%s",def->args.s1[i]);
- printf(")");
- }
-
- printf("\n");
- }
-
- if(def->comment && option_raw)
- printf("<<<\n%s\n>>>\n",def->comment);
- if(option_warn&WARN_COMMENT && !def->comment)
- printf("Warning %16s : #Define %s does not have a comment.\n",filename,def->name);
-
- for(i=0;i<def->args.n;i++)
- {
- if(option_raw)
- if(def->args.s2[i])
- printf("Arguments: %s <<<%s>>>\n",def->args.s1[i],def->args.s2[i]);
- else
- printf("Arguments: %s\n",def->args.s1[i]);
- if(option_warn&WARN_COMMENT && !def->args.s2[i])
- printf("Warning %16s : #Define %s has an argument %s with no comment.\n",filename,def->name,def->args.s1[i]);
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a Typedef structure out.
-
- Typedef type The Typedef structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawTypedef(Typedef type)
- {
- if(option_raw)
- {
- if(type->type)
- printf("\nTYPEDEF : '%s'\n",type->name),
- printf("Type: %s\n",type->type);
- else
- printf("\nTYPE : '%s'\n",type->name);
-
- if(type->typexref)
- printf("See: %s %s\n",type->typexref->type?"Typedef":"Type",type->typexref->name);
- }
-
- if(type->comment && option_raw)
- printf("<<<\n%s\n>>>\n",type->comment);
- if(option_warn&WARN_COMMENT && !type->comment)
- printf("Warning %16s : Typedef %s does not have a comment.\n",filename,type->name);
-
- if(type->sutype)
- WriteWarnRawStructUnion(type->sutype,0,type->sutype);
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a structure / union / enum out.
-
- StructUnion su The structure / union / enum to write.
-
- int depth The depth within the structure.
-
- StructUnion base The base struct union that this one is part of.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawStructUnion(StructUnion su, int depth,StructUnion base)
- {
- int i;
- char* splitsu=NULL;
-
- if(option_warn&WARN_COMMENT && depth && !su->comment)
- printf("Struct/Union component %s in %s does not have a comment.\n",su->name,base->name);
-
- splitsu=strstr(su->name,"{...}");
- if(splitsu) splitsu[-1]=0;
-
- if(option_raw)
- {
- for(i=0;i<depth;i++) printf(" ");
- if(depth && su->comment)
- printf("%s <<<%s>>>\n",su->name,su->comment);
- else
- printf("%s\n",su->name);
- }
-
- if(su->comps)
- {
- if(option_raw)
- {
- for(i=0;i<depth;i++) printf(" ");
- printf(" {\n");
- }
- for(i=0;i<su->n_comp;i++)
- WriteWarnRawStructUnion(su->comps[i],depth+1,base);
- if(option_raw)
- {
- for(i=0;i<depth;i++) printf(" ");
- printf(" }\n");
- if(splitsu)
- {
- for(i=0;i<depth;i++) printf(" ");
- printf("%s\n",&splitsu[6]);
- }
- }
- }
-
- if(splitsu) splitsu[-1]=' ';
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a Variable structure out.
-
- Variable var The Variable structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawVariable(Variable var)
- {
- int i;
-
- if(option_raw)
- {
- printf("\nVARIABLE : %s ",var->name);
- switch(var->scope)
- {
- case LOCAL: printf("[Local]\n"); break;
- case GLOBAL: printf("[Global definition]\n"); break;
- case EXTERNAL: printf("[External]\n"); break;
- case EXTERN_H: printf("[External from header file]\n"); break;
- case EXTERNAL+GLOBAL: printf("[Global and External]\n"); break;
- case EXTERN_H+GLOBAL: printf("[Global and External from header file]\n"); break;
- default: printf("[Warning strange scope (%d)]\n",var->scope);
- }
- printf("Type: %s\n",var->type);
-
- if(var->comment)
- printf("<<<\n%s\n>>>\n",var->comment);
- }
-
- if(option_warn&WARN_COMMENT && !var->comment && (var->scope&(GLOBAL|LOCAL|EXTERNAL) || option_raw))
- printf("Warning %16s : Variable %s does not have a comment.\n",filename,var->name);
-
- if(option_xref&XREF_VAR)
- {
- if(option_raw)
- {
- if(var->scope&EXTERNAL && var->defined)
- printf("Declared global in '%s'\n",var->defined);
-
- if(var->scope&(GLOBAL|LOCAL))
- {
- for(i=0;i<var->visible.n;i++)
- printf("Visible in %s\n",var->visible.s[i]);
-
- for(i=0;i<var->used.n;i++)
- if(var->used.s[i][0]=='$')
- printf("Used in %s\n",&var->used.s[i][1]);
- else
- printf("Used in %s\n",var->used.s[i]);
- }
- }
-
- if(option_warn&WARN_XREF)
- {
- if(var->scope&EXTERNAL && !var->defined)
- printf("Warning %16s : Variable %s has an unknown global definition.\n",filename,var->name);
-
- if(var->scope&(GLOBAL|LOCAL|EXTERNAL) && !var->used.n)
- printf("Warning %16s : Variable %s is not used anywhere.\n",filename,var->name);
-
- if(var->scope&(GLOBAL|EXTERNAL) && var->used.n)
- {
- char* this_file=ConcatStrings(2," : ",filename);
- int is_used_elsewhere=0,is_used_here=0;
- for(i=0;i<var->used.n;i++)
- if((var->used.s[i][0]=='$' && !strcmp(filename,&var->used.s[i][1])) ||
- strstr(var->used.s[i],this_file))
- is_used_here=1;
- else
- is_used_elsewhere=1;
- if(!is_used_elsewhere)
- printf("Warning %16s : Variable %s is %s but only used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
- if(!is_used_here)
- printf("Warning %16s : Variable %s is %s but not used in this file.\n",filename,var->name,var->scope&GLOBAL?"global":"extern");
- }
- }
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write a Function structure out.
-
- Function func The Function structure to output.
- ++++++++++++++++++++++++++++++++++++++*/
-
- static void WriteWarnRawFunction(Function func)
- {
- int i;
-
- if(option_raw)
- {
- printf("\nFUNCTION : %s ",func->name);
- switch(func->scope)
- {
- case LOCAL: printf("[Local]\n"); break;
- case GLOBAL: printf("[Global]\n"); break;
- case LOCAL+INLINED: printf("[Local inline]\n"); break;
- case INLINED: printf("[inline]\n"); break;
- default: printf("[Warning strange scope (%d)]\n",func->scope);
- }
- }
-
- if(func->comment && option_raw)
- printf("<<<\n%s\n>>>\n",func->comment);
- if(option_warn&WARN_COMMENT && !func->comment)
- printf("Warning %16s : Function %s() does not have a comment.\n",filename,func->name);
-
- if(func->protofile && option_raw)
- printf("Prototyped in %s\n",func->protofile);
- if(option_warn&WARN_XREF && !func->protofile)
- printf("Warning %16s : Function %s() is not prototyped.\n",filename,func->name);
-
- if(option_raw)
- if(func->cret)
- printf("Type: %s <<<%s>>>\n",func->type,func->cret);
- else
- printf("Type: %s\n",func->type);
- if(option_warn&WARN_COMMENT && !func->cret && strncmp("void ",func->type,5))
- printf("Warning %16s : Function %s() has a return value with no comment.\n",filename,func->name);
-
- for(i=0;i<func->args.n;i++)
- {
- if(option_raw)
- if(func->args.s2[i])
- printf("Arguments: %s <<<%s>>>\n",func->args.s1[i],func->args.s2[i]);
- else
- printf("Arguments: %s\n",func->args.s1[i]);
- if(option_warn&WARN_COMMENT && !func->args.s2[i] && strcmp("void",func->args.s1[i]))
- printf("Warning %16s : Function %s() has an argument %s with no comment.\n",filename,func->name,func->args.s1[i]);
- }
-
- if(option_xref&XREF_FUNC)
- {
- for(i=0;i<func->calls.n;i++)
- {
- if(option_raw)
- printf("Calls %s\n",func->calls.s[i]);
- #if 0 /* Too verbose */
- if(option_warn&WARN_XREF && !strchr(func->calls.s[i],':'))
- printf("Warning %16s : Function %s() calls function %s() whose definition is unknown.\n",filename,func->name,func->calls.s[i]);
- #endif
- }
-
- if(option_raw)
- for(i=0;i<func->called.n;i++)
- printf("Called from %s\n",func->called.s[i]);
-
- if(option_raw)
- for(i=0;i<func->used.n;i++)
- {
- if(func->used.s[i][0]=='$')
- printf("Used in %s\n",&func->used.s[i][1]);
- else
- printf("Used in %s\n",func->used.s[i]);
- }
-
- for(i=0;i<func->f_refs.n;i++)
- {
- if(option_raw)
- printf("References Function %s\n",func->f_refs.s[i]);
- if(option_warn&WARN_XREF && !strchr(func->f_refs.s[i],':'))
- printf("Warning %16s : Function %s() references function %s() whose definition is unknown.\n",filename,func->name,func->f_refs.s[i]);
- }
- }
-
- if(option_xref&XREF_VAR)
- for(i=0;i<func->v_refs.n;i++)
- {
- if(option_raw)
- printf("References Variable %s\n",func->v_refs.s[i]);
- if(option_warn&WARN_XREF && !strchr(func->v_refs.s[i],':'))
- printf("Warning %16s : Function %s() references variable %s whose definition is unknown.\n",filename,func->name,func->v_refs.s[i]);
- }
-
-
- if(option_warn&WARN_XREF)
- {
- if(!func->used.n && !func->called.n)
- printf("Warning %16s : Function %s() is not used anywhere.\n",filename,func->name);
-
- if(func->scope&GLOBAL && (func->called.n || func->used.n))
- {
- char* this_file=ConcatStrings(2," : ",filename);
- int is_used_elsewhere=0;
- for(i=0;i<func->called.n;i++)
- if(!strstr(func->called.s[i],this_file))
- {is_used_elsewhere=1;break;}
- for(i=0;i<func->used.n;i++)
- if((func->used.s[i][0]=='$' && strcmp(filename,&func->used.s[i][1])) ||
- !strstr(func->used.s[i],this_file))
- {is_used_elsewhere=1;break;}
- if(!is_used_elsewhere)
- printf("Warning %16s : Function %s() is global but is only used in this file.\n",filename,func->name);
- }
- }
- }
-
-
- /*++++++++++++++++++++++++++++++++++++++
- Write out a raw version of the appendix.
-
- StringList* files The list of files to write.
-
- StringList* funcs The list of functions to write.
-
- StringList* vars The list of variables to write.
-
- StringList* types The list of types to write.
- ++++++++++++++++++++++++++++++++++++++*/
-
- void WriteWarnRawAppendix(StringList* files,StringList* funcs,StringList* vars,StringList* types)
- {
- int i;
-
- /* Write out the appendix of files. */
-
- if(option_index&INDEX_FILE)
- if(files->n)
- {
- printf("\nAppendix - Files\n\n");
- for(i=0;i<files->n;i++)
- printf("%s\n",files->s[i]);
- }
- else
- if(option_warn&WARN_XREF)
- printf("Warning Index : No global files to index.\n");
-
- /* Write out the appendix of functions. */
-
- if(option_index&INDEX_FUNC)
- if(funcs->n)
- {
- printf("\nAppendix - Global Functions\n\n");
- for(i=0;i<funcs->n;i++)
- printf("%s\n",funcs->s[i]);
- }
- else
- if(option_warn&WARN_XREF)
- printf("Warning Index : No global functions to index.\n");
-
- /* Write out the appendix of variables. */
-
- if(option_index&INDEX_VAR)
- if(vars->n)
- {
- printf("\nAppendix - Global Variables\n\n");
- for(i=0;i<vars->n;i++)
- printf("%s\n",vars->s[i]);
- }
- else
- if(option_warn&WARN_XREF)
- printf("Warning Index : No global variables to index.\n");
-
- /* Write out the appendix of types. */
-
- if(option_index&INDEX_TYPE)
- if(types->n)
- {
- printf("\nAppendix - Defined Types\n\n");
- for(i=0;i<types->n;i++)
- printf("%s\n",types->s[i]);
- }
- else
- if(option_warn&WARN_XREF)
- printf("Warning Index : No types to index.\n");
- }
-